package com.eric.similarity;

import java.io.IOException;
import java.util.AbstractMap;
import java.util.Iterator;
import java.util.Map.Entry;

import com.eric.similarity.cluster.MapClusterer;
import com.eric.similarity.pipe.FilePipe;
import com.webssky.jcseg.main.JSeg;

public class HashSimilarity<K,S extends Number> extends Similarity<K,S> {
	int dis;

	public static void main(String[] args) throws IOException {
		HashSimilarity<String,Integer> hs = new HashSimilarity<String,Integer>("/home/eric/tmp/costest");
		hs.calHashDistance("hello world,this is us","hello world,this is china");
        System.out.println(hs.dis);
        
        hs.SimOfFiles("/home/eric/tmp/costest");
        
        System.out.println(hs.Sim);
        
        MapClusterer<String,String> cluster = new MapClusterer<String,String>();
        System.out.println(hs.SimMap);
        System.out.println(cluster.Cluster(hs.SimMap));
        
    }
	
	public HashSimilarity () {}
	
	@SuppressWarnings("unchecked")
	public HashSimilarity (String path) throws IOException {
//		SimOfFiles(path);
		pipe = new FilePipe<K>(path);
		calSim ();
		ConfidentMap();
		SimMap();
	}
	
	public int calHashDistance (String s1,String s2) throws IOException {
		JSeg seg = new JSeg();
		SimHash hash1 = new SimHash(s1);
        SimHash hash2 = new SimHash(s2);
        
        dis = hash1.getDistance(hash1.strSimHash,hash2.strSimHash);
        
        return dis;
	}
	
	public void ConfidentMap () {
		Iterator<Entry<Entry<K, K>, S>> it = Sim.entrySet().iterator();
		while (it.hasNext()) {
			Entry<Entry<K,K>,S> entry = it.next();
			if ( entry.getValue().doubleValue() < THRESHOLD * 10.0) ConfidentMap.put(entry.getKey(), entry.getValue());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void putSim(K file1, K file2, K content1, K content2) {
		// TODO Auto-generated method stub
		try {
			Sim.put(new AbstractMap.SimpleEntry<K,K>(file1,file2),
					(S) new Integer(calHashDistance((String)content1,(String)content2))
					);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
